package com.xy.xylive.api;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.xylive.aop.Decrypt;
import com.xy.xylive.aop.Encrypt;
import com.xy.xylive.common.ErrorCode;
import com.xy.xylive.common.PageUtils;
import com.xy.xylive.common.ServiceException;
import com.xy.xylive.domain.Audience;
import com.xy.xylive.domain.AudienceSetting;
import com.xy.xylive.mapper.AudienceSettingMapper;
import com.xy.xylive.service.IAudienceService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import lombok.extern.slf4j.Slf4j;

/**
 * 观众Controller
 * 
 * @author ghostsf
 * @date 2024-05-19
 */
@RestController
@Slf4j
@RequestMapping("/audience")
public class AudienceController
{
    @Autowired
    private IAudienceService audienceService;

    /**
     * 查询观众列表
     */
    @GetMapping("/list")
    public PageR<Audience> list(Audience audience)
    {
        PageUtils.startPage();
        List<Audience> list = audienceService.selectAudienceList(audience);
        return PageR.ok(list);
    }

    /**
     * 获取观众详细信息
     */
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable Integer id)
    {
        return R.ok(audienceService.selectAudienceById(id));
    }

    /**
     * 新增观众
     */
    @PostMapping
    @Encrypt
    public R add(@RequestBody @Decrypt Audience audience)
    {
        return R.ok(audienceService.insertAudience(audience));
    }

    /**
     * 修改观众
     */
    @PutMapping
    public R edit(@RequestBody Audience audience)
    {
        return R.ok(audienceService.updateAudience(audience));
    }

    /**
     * 删除观众
     */
	@DeleteMapping("/{ids}")
    public R remove(@PathVariable Integer[] ids)
    {
        return R.ok(audienceService.deleteAudienceByIds(ids));
    }


    @Autowired
    private AudienceSettingMapper audienceSettingMapper;

    /**
     * 获取观众详细信息设置
     */
    @GetMapping(value = "/setting/{liveId}")
    @Encrypt
    public R getSettingInfo(@PathVariable Integer liveId)
    {
        if(liveId == null) {
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        return R.ok(audienceSettingMapper.selectAudienceSettingByLiveId(liveId));
    }

    /**
     * 修改观众详细信息设置
     */
    @PutMapping(value = "/setting")
    public R updateAudienceSetting(@RequestBody AudienceSetting audienceSettingReq)
    {
        if(audienceSettingReq.getId() == null) {
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        AudienceSetting audienceSetting = audienceSettingMapper.selectAudienceSettingById(audienceSettingReq.getId());
        if(audienceSetting == null) {
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        BeanUtil.copyProperties(audienceSettingReq, audienceSetting);
        audienceSetting.setUpdateTime(new Date());
        return R.ok(audienceSettingMapper.updateAudienceSetting(audienceSetting));
    }

    /**
     * 导出
     * @param audience
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody Audience audience) throws IOException {
        List<Audience> list = audienceService.selectAudienceList(audience);
        ExcelWriter writer = ExcelUtil.getWriter(true);

        //自定义标题别名
        writer.addHeaderAlias("id", "ID");
        writer.setColumnWidth(0, 30);

        writer.addHeaderAlias("name", "姓名");
        writer.setColumnWidth(1, 30);

        writer.addHeaderAlias("phone", "电话");
        writer.setColumnWidth(2, 30);

        writer.addHeaderAlias("unit", "单位");
        writer.setColumnWidth(3, 30);

        writer.addHeaderAlias("department", "部门");
        writer.setColumnWidth(4, 30);

        writer.addHeaderAlias("position", "职位");
        writer.setColumnWidth(5, 30);

        writer.addHeaderAlias("email", "邮箱");
        writer.setColumnWidth(6, 30);

        writer.addHeaderAlias("gender", "性别");
        writer.setColumnWidth(7, 30);

        writer.addHeaderAlias("qq", "QQ");
        writer.setColumnWidth(8, 30);

        writer.addHeaderAlias("wx", "微信");
        writer.setColumnWidth(9, 30);

        writer.setOnlyAlias(true);
        writer.autoSizeColumnAll();

        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=test.xlsx");
        ServletOutputStream out=response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }
}
